package cn.liyicun.cp.cpweb.controller.api;

import cn.hutool.http.HttpUtil;
import cn.liyicun.cp.cpweb.base.Result;
import cn.liyicun.cp.cpweb.controller.BaseController;
import cn.liyicun.cp.cpweb.entity.LoginInfo;
import cn.liyicun.cp.cpweb.service.UserInfoService;
import cn.liyicun.cp.cpweb.util.CPConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping(value = {CPConstant.ROOT_PHONE + "/user"})
public class UserInfoApiController extends BaseController {

    @Autowired
    UserInfoService userInfoService;
@Value("${cp.default.test}")
boolean test;

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Result<?> register(@RequestParam(value = "userName", required = true) String userName,
                              @RequestParam(value = "passwd", required = true) String passwd,
                              @RequestParam(value = "phone", required = true) String phone,
                              @RequestParam(value = "qq", required = true) String qq) {

        Long pid = 0L;
        userInfoService.create(userName, passwd, phone, qq, pid);
        // 跳转到 templates 目录下的 uploadimg.html
        return Result.success("增加成功!");
    }

    @RequestMapping(value = "/updateBank", method = RequestMethod.POST)
    public Result<?> updateBank(HttpServletRequest request,
                                @RequestParam(value = "bankAddr", required = true) String bankAddr,
                                @RequestParam(value = "bankCard", required = true) String bankCard,
                                @RequestParam(value = "bankName", required = true) String bankName,
                                @RequestParam(value = "bankUser", required = true) String bankUser) {
        Long uid = getLoginById(request);


        userInfoService.userUpdateBank(uid, bankAddr, bankCard, bankName, bankUser);
        return Result.success("修改成功!");
    }

    @RequestMapping(value = "/changePasswd", method = RequestMethod.POST)
    public Result<?> changePasswd(HttpServletRequest request,
                                  @RequestParam(value = "oldPasswd", required = true) String oldPasswd,
                                  @RequestParam(value = "newPasswd", required = true) String newPasswd,
                                  @RequestParam(value = "newPasswd2", required = true) String newPasswd2) {
        Long uid = getLoginById(request);
        compare(!newPasswd.equals(newPasswd2), "新密码二次密码不一致");
        compare(newPasswd.equals(oldPasswd), "新旧密码不能一致!");
        userInfoService.changePasswd(uid, oldPasswd, newPasswd);
        return Result.success("修改成功!");
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result<?> login(HttpServletRequest request,
                           @RequestParam(value = "userName", required = true) String userName,
                           @RequestParam(value = "passwd", required = true) String passwd,
                           @RequestParam(value = "captcha", required = true) String captcha) {
if(!test) {
    Object data = getAttribute(request, CPConstant.captcha);
    compare(data == null, "验证码失效!");
    removeAttribute(request, CPConstant.captcha);
    compare(!data.toString().equalsIgnoreCase(captcha), "验证码错误!");
}
        String ip = HttpUtil.getClientIP(request);
        LoginInfo info = userInfoService.login(userName, passwd, captcha, ip);
        setLoginInfo(request, info);
        return Result.success("登陆成功!");
    }


    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public void logout(HttpServletResponse response, Map<String, Object> map, HttpServletRequest request) throws IOException {
        logout(request);
        response.sendRedirect("/");
    }

    @RequestMapping(value = "/getCaptcha")
    public Result<?> getCaptcha(HttpServletRequest request) {
        return Result.success("修改成功!");
    }

}
